package com.irdstudio.apicenter.console.controller.appmgr;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.frame.core.jdbc.container.PageInfo;

/**
 * app功能管理 1、消息推送 2、营业网点管理
 * 
 * @author zs.ligm
 */
@Controller
@RequestMapping(value = "/auth")
public class AppFuncManager extends MacmsBaseAction {

	/**
	 * 消息推送信息列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appPushMessageList")
	public String appPushMessageList(HttpServletRequest request)
			throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中
		String sql = "select * from app_push_task where app_id=? order by push_time desc";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("aptList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("aptList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appPushMessageList";
	}

	/**
	 * 营业网点列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appSaleSiteList")
	public String appSaleSiteList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String saleSiteId = (request.getParameter("saleSiteId") == null) ? ""
				: request.getParameter("saleSiteId");
		String saleSiteName = (request.getParameter("saleSiteName") == null) ? ""
				: request.getParameter("saleSiteName");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 将查询条件放入上下文中
		request.setAttribute("saleSiteId", saleSiteId);
		request.setAttribute("saleSiteName", saleSiteName);

		// 查询数据并放入到上下文中
		String sql = "select * from app_sale_site where app_id=? "
				+ "and sale_site_id like '%" + saleSiteId
				+ "%' and sale_site_name like '%" + saleSiteName
				+ "%' order by sale_site_id";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("assList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("assList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appSaleSiteList";
	}

	/**
	 * 系统公告列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appNoticeList")
	public String appNoticeList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String noticeTitle = (request.getParameter("noticetitle") == null) ? ""
				: request.getParameter("noticetitle");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 将查询条件放入上下文中
		request.setAttribute("noticetitle", noticeTitle);

		// 查询数据并放入到上下文中
		String sql = "select * from app_notice_show where app_id=? "
				+ "and notice_title like '%" + noticeTitle
				+ "%' order by notice_date desc";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("notiList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("notiList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appNoticeList";
	}
	
	/**
	 * 数据字典管理
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appDicList")
	public String appDicList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String cnname = (request.getParameter("cnname") == null) ? ""
				: request.getParameter("cnname");
		String enname = (request.getParameter("enname") == null) ? ""
				: request.getParameter("enname");
		// 将查询条件放入上下文中
		request.setAttribute("cnname", cnname);
		request.setAttribute("enname", enname);

		// 查询数据并放入到上下文中
		String sql = "select * from s_dic where cnname like '%" + cnname+"%' "
				+ " and enname like '%" + enname+"%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("dicList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] {}));
		context.addPageInfo("dicList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		return "auth/appDicList";
	}
	
	/**
	 * APP版本管理
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appVersionList")
	public String appVersionList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		String appVersionId = (request.getParameter("appVersionId") == null) ? ""
				: request.getParameter("appVersionId");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 将查询条件放入上下文中
		request.setAttribute("appVersionId", appVersionId);

		// 查询数据并放入到上下文中
		String sql = "select * from app_version where app_id=? "
				+ "and app_version_id like '%" + appVersionId
				+ "%' order by app_version_id";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("assList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("assList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appVersionList";
	}
	
	
	
	
	
	
	/**
	 * 微信菜单配置列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "wxMenuConfig")
	public String wxMenuConfig(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String wx_menu_id = request.getParameter("wx_menu_id") == null ? "" : request.getParameter("wx_menu_id");
		String wx_menu_name = request.getParameter("wx_menu_name") == null ? "" : request.getParameter("wx_menu_name");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中  and wx_menu_id like 
		String sql = "select * from wx_menu_define where app_id=? and wx_menu_id like '%"+wx_menu_id+"%'"+
				" and wx_menu_name like '%"+wx_menu_name+"%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("wxList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("wxList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/wxMenuConfig";
	}
	
	
	/**
	 * 微信菜单响应配置
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "wxRespAction")
	public String wxRespAction(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String wx_resp_actionid = request.getParameter("wx_resp_actionid") == null ? "" : request.getParameter("wx_resp_actionid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中  and wx_menu_id like 
		String sql = "select * from wx_resp_action where app_id=? and wx_resp_actionid like '%"+wx_resp_actionid+"%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("wxList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("wxList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/wxRespAction";
	}
	
	
	
	/**
	 * 微信菜单响应详情
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "wxRespDetail")
	public String wxRespDetail(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String wx_resp_actionid = request.getParameter("wx_resp_actionid") == null ? "" : request.getParameter("wx_resp_actionid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中  and wx_menu_id like 
		String sql = "select * from wx_resp_detail where wx_resp_actionid like '%"+wx_resp_actionid+"%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("wxList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] {}));
		context.addPageInfo("wxList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/wxRespDetail";
	}
	
	/**
	 * 微信模版消息映射
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "wxModelMessage")
	public String wxModelMessage(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String dxModelId = request.getParameter("dxModelId") == null ? "" : request.getParameter("dxModelId");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中  and wx_menu_id like 
		String sql = "select * from WX_MODEL_DEFINE where dx_model_id like '%"+dxModelId+"%' order by dx_model_id";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("wxList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] {}));
		context.addPageInfo("wxList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/wxModelMessage";
	}
	
	
	/**
	 * 微信模版消息配置
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "wxModelConfig")
	public String wxModelConfig(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String dxModelId = request.getParameter("dxModelId") == null ? "" : request.getParameter("dxModelId");
		String modelLineNum = request.getParameter("modelLineNum") == null ? "" : request.getParameter("modelLineNum");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中  and wx_menu_id like 
		String sql = "select * from wx_model_detail where dx_model_id like '%"+dxModelId+"%' and model_line_num like '%"+modelLineNum+"%' order by dx_model_id,model_line_num";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("wxList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] {}));
		context.addPageInfo("wxList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/wxModelConfig";
	}
	
	/**
	 * 微信模版消息预览
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "preseeWxModel")
	public String wxModelPresee(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String dxModelId = request.getParameter("dxModelId") == null ? "" : request.getParameter("dxModelId");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中  
		String sql = "select * from wx_model_detail where dx_model_id = "+dxModelId+" order by model_line_num";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("wxList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] {}));
		context.addPageInfo("wxList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/preseeWxModel";
	}
	
	
	
}
